import md5 from 'crypto-js/md5';
export default{
    data(){
        return{
            comId:null,
            modal1:false,  //对话框
            ids:[],  //多选框
            cid:null,  //树形菜单的id
            list:[],  //列表：users  product  order 
            filter:{  //过滤器
                page:1,
                limit:10,
                name:'',
                total:0
            }
        }
    },
    methods:{
        //增
        addData(cid){
            this.btn = 'add';
            this.modal1 = true;
            this.formValidate = {};   //添加后清空数据
        },
        handleSubmit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    if(this.btn == 'add'){   //点击确认时判断是添加还是编辑
                        if(this.module === "news"){   //判断是新闻页还是用户管理页
                            if(this.cid != null){  //cid不为空，cid为父元素id，添加新闻
                                this.$http({
                                    method:'POST',
                                    url:`http://localhost:3000/${this.module}/data/`,
                                    data:{
                                        cateId:this.cid,
                                        title:this.formValidate.title,
                                        ftitle:this.formValidate.ftitle,
                                        author:this.formValidate.author,
                                        data: this.formValidate.data,
                                        content: this.formValidate.content
                                    }
                                }).then(res => {
                                    // this.getTree(this.forValidate.type);
                                })
                            }else{ 
                                console.log('请选择根节点。');
                            }
                        }else if(this.module === "users" || this.module === "commend"){    //用户管理页
                            // console.log(md5(this.formValidate.password).toString());
                            //密码加密
                            this.formValidate.password = md5(this.formValidate.password).toString(); 
                            this.$http({
                                method:'POST',
                                url:`http://localhost:3000/${this.module}/data/`,
                                data:this.formValidate
                            }).then(res => {
                                // console.log('..........');
                            })
                        }
                        this.getData();   //刷新
                    }else if(this.btn == 'edit'){   //点击确定修改按钮，发送修改后的数据
                        // this.editData(this.formValidate._id);
                        // console.log(this.formValidate._id)
                        this.formValidate.password = md5(this.formValidate.password).toString(); 
                        this.$http({
                            method:'POST',
                            url:`http://localhost:3000/${this.module}/data/` + this.formValidate._id,
                            data:this.formValidate
                        }).then((response) => {
                            this.formValidate = response.data;
                            // console.log(response);
                            // console.log(this.formValidate)
                        })
                    }
                    this.getData();   //刷新
                    this.$Message.success('提交成功!');
                } else {
                    this.$Message.error('表单验证失败!');
                }
            })
        },
        //删
        remove (id) {
            this.$Modal.confirm({
                title: '确认对话框标题',
                content: '<p>确定要删除信息吗？</p>',
                onOk: () => {
                    this.$http({
                        method:'DELETE',
                        url:`http://localhost:3000/${this.module}/data/` + id,
                        // data:this.formValidate
                    }).then((response) => {
                        // console.log(response);
                        this.formValidate = response.data;
                    })
                    this.list.splice(id, 1);
                    this.getData();
                },
                onCancel: () => {
                    this.$Message.info('点击了取消');
                }
            });
        },
        //删多个
        removeData(){
            // console.log(this.ids)
            this.$Modal.confirm({
                title: '确认对话框标题',
                content: '<p>确定要删除信息吗？</p>',
                onOk: () => {
                    for(var i=0;i<this.ids.length;i++){
                        // console.log(this.ids[i]._id)
                        var id = this.ids[i]._id;
                        this.$http({
                            method:'DELETE',
                            url:`http://localhost:3000/${this.module}/data/` + id
                        }).then((response) => {
                            this.formValidate = response.data;
                        })
                        this.list.splice(id, 1);
                        this.getData();
                    }
                },
                onCancel: () => {
                    this.$Message.info('点击了取消');
                }
            });
        },
        //点击修改，弹出对话框，显示数据
        editData(id){
            this.btn = 'edit';
            this.modal1 = true;
            this.formValidate.password = md5(this.formValidate.password).toString(); 
            this.$http({
                method:'GET',
                url:`http://localhost:3000/${this.module}/data/` + id
                // data:this.formValidate
            }).then((response) => {
                this.formValidate = response.data;
                // console.log(response);
                // console.log(this.formValidate)
            })
        },
        //查
        onPageSizeChange(pageSize){
            this.filter.limit = pageSize;
            this.getData();
        },
        changePage(page){
            this.filter.page = page;
            this.getData();
        },
        getData(){   //开始显示数据
            // console.log(this.module)
            this.$http({
                method:'post',
                url:`http://localhost:3000/${this.module}/list`,
                data:this.filter
            }).then(res => {
                console.log(res)
                this.list = res.data.rows;
                this.filter.total = res.data.total;
            })
        },
        search(){
            this.getData();
        }
    },
    created(){
        this.getData();
    }
}